//
//  ViewController.m
//  UsingOfCoredata
//
//  Created by 苹果电脑 on 16/9/13.
//  Copyright © 2016年 wyzc. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "AppDelegate.h"

@interface ViewController ()
{
    NSManagedObjectContext *_context;
}
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //获取上下文对象
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    _context = [delegate managedObjectContext];
    
}
#pragma mark -- 存储数据
- (IBAction)save:(id)sender {
    
    //创建实体对象
    Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:_context];
    //设置属性
    [student setValue:@([_idTextField.text integerValue])forKey:@"id"];
    [student setValue:_nameTextField.text forKey:@"name"];
    [student setValue:@([_ageTextField.text integerValue])forKey:@"age"];
    //必须通过上下文要保存
    BOOL isSave = [_context save:nil];
    NSLog(@"%@", isSave?@"保存成功":@"保存失败");
    
}
#pragma mark -- 查询数据
- (IBAction)quary:(id)sender {
    [self executeFetch];
}

#pragma mark -- 更新数据
- (IBAction)update:(id)sender {
    NSArray *fetchResult = [self executeFetch];
    __weak ViewController *weakSelf = self;
    __weak NSManagedObjectContext *weakContent = _context;
    [fetchResult enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //查询获取要更新的对象
        if ([[obj valueForKey:@"id"] integerValue] == [weakSelf.idTextField.text integerValue]) {
            //更新属性值
            [obj setValue:weakSelf.nameTextField.text forKey:@"name"];
            [obj setValue:@([weakSelf.ageTextField.text integerValue]) forKey:@"age"];
            //保存更新
            BOOL isSaveSuccess = [weakContent save:nil];
            NSLog(@"%@",isSaveSuccess?@"更新成功":@"更新失败");
        }
    }];
}

#pragma mark -- 删除数据
- (IBAction)delete:(id)sender {
    //查询
    NSArray * fecthArray = [self executeFetch];
    __weak ViewController *weakSelf = self;
    __weak NSManagedObjectContext *weakContext = _context;
    [fecthArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //查找到和输入名称一致的数据，删除
        if ([[obj valueForKey:@"name"] isEqualToString:weakSelf.nameTextField.text]){
            //删除要删除的对象
            [weakContext deleteObject:obj];
            
            BOOL isDeleteSave = [weakContext save:nil];
            NSLog(@"%@",isDeleteSave?@"成功":@"失败");

        }
        }];
}

#pragma mark -- 查询
-(NSArray *)executeFetch{
    //创建查询对象
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    //通过上下文查询数据
    NSArray *requestArray = [_context executeFetchRequest:request error:nil];
    NSLog(@"%@",requestArray);
    //遍历数组获取数据
    [requestArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%ld  %@  %ld",[[obj valueForKey:@"id"] integerValue],[obj valueForKey:@"name"],[[obj valueForKey:@"age"] integerValue]);
        
    }];
    return requestArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

